﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace CAUNetLogin.NetUtil.ReturnMessage
{
    public class SucceedMessage
    {
        public string NID = "";//用户标识
        public long timeUsed = 0;//本月已使用时间(Minute)
        public double flowUsed = 0;//本月已使用流量(MB)
        public double balance = 0;//余额(CNY)
        public string xip = "0.0.0.0";//外网映射IP
        public string lip = "0.0.0.0";//上次登录IP
        public string stime = "";//上次登录时间
        public string etime = "";//上次注销时间
        public double flowRemained = 0;//剩余可用流量
        public string uid = "";//学号


        public string lastErrorMessage = "";
        public string returnMessage = "";



        private long flow = 0;
        private int fsele = 0;
        private long fee = 0;
        private int xsele = 0;
        private string oltime = "";
        private long olflow = 0;

        public bool ParseMsg(string html)
        {
            
            try
            {
                string tempMain = Regex.Replace(html.Substring(120, 500), "\r\n", "");//去除回车换行
                string pattern = @".*time='(?<time>\d*)\s*';flow='(?<flow>\d*)\s*';fsele=(?<fsele>\d*);fee='(?<fee>\d*)\s*';xsele=(?<xsele>\d*);xip='(?<xip>.*)';.*uid='(?<uid>.*)';pwd.*NID='(?<NID>.*)';.*oltime=(?<oltime>\d*);olflow=(?<olflow>\d*);lip='(?<lip>.*)';stime='(?<stime>.*)';etime='(?<etime>.*)';";//
                Regex r = new Regex(pattern);
                if (r.IsMatch(tempMain))
                {
                    Match match = r.Match(tempMain);
                    Int64.TryParse(match.Groups["time"].ToString(), out timeUsed);//本月已使用时间(Minute)
                    Int64.TryParse(match.Groups["flow"].ToString(), out flow);
                    Int32.TryParse(match.Groups["fsele"].ToString(), out fsele);
                    Int64.TryParse(match.Groups["fee"].ToString(), out fee);
                    NID = match.Groups["NID"].ToString();//用户标识
                    oltime = match.Groups["oltime"].ToString();
                    Int64.TryParse(match.Groups["olflow"].ToString(),out olflow);
                    lip = match.Groups["lip"].ToString();//上次登录ip
                    stime = match.Groups["stime"].ToString();//上次登录时间
                    etime = match.Groups["etime"].ToString();//上次注销时间
                    Int32.TryParse(match.Groups["xsele"].ToString(),out xsele);
                    xip = match.Groups["xip"].ToString();//外网IP
                    uid = match.Groups["uid"].ToString();//用户账号
                    if (fsele == 1)
                    {
                        balance = (fee - fee % 100) / 10000.0;//余额
                    }
                    flowUsed = flow / 1024.0;//MB
                    flowRemained = olflow / 1024.0;//MB
                }
                
            }catch(Exception ex)
            {
                lastErrorMessage = ex.Message;
                return false;
            }
            return true;
        }
    }
}
